package com.chuanyang.driverh.mainhome.fragment;

import android.content.Intent;
import android.support.annotation.IdRes;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.chuanyang.driverh.R;
import com.chuanyang.driverh.base.BaseListAdapter;
import com.chuanyang.driverh.base.BaseListFragment;
import com.chuanyang.driverh.mainhome.FilterActivity;
import com.chuanyang.driverh.mainhome.adapter.WaitTransAdapter;
import com.chuanyang.driverh.response2.RequestFilter;
import com.chuanyang.driverh.response2.ResultWaitTransDriver;
import com.chuanyang.driverh.response2.ResultWaitTransTop;
import com.chuanyang.driverh.utils.ConstUtil;
import com.chuanyang.driverh.utils.DialogUtil;
import com.chuanyang.driverh.utils.PrintUtil;
import com.chuanyang.driverh.utils.SharedPrefUtil;
import com.chuanyang.driverh.utils.StringUtil;
import com.chuanyang.driverh.utils.eventbus.FilterEvent;
import com.chuanyang.driverh.utils.eventbus.RefleshFragEvent;
import com.chuanyang.driverh.utils.net.JsonGenericsSerializator;
import com.chuanyang.driverh.utils.net.MyCallback;
import com.chuanyang.driverh.utils.net.MyHttpUtil;

import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;

public class WaitTransFragment extends BaseListFragment {
    private WaitTransAdapter listAdapter;

    private View title_bar;
    private RadioGroup rg_top;
    private RadioButton rb0,rb1,rb2,rb3;
    private RequestFilter filter;
    private String showType="1";//(1:全部计划，2:当日/月实发, 3:单日/月待发, 4:当日/月在运)

    @Override
    public BaseListAdapter createAdapter() {
        return listAdapter = new WaitTransAdapter(getActivity());
    }

    @Override
    public void initView(View view) {
        super.initView(view);
        title_bar = View.inflate(getContext(), R.layout.home_header_get_order, null);
        rg_top=(RadioGroup) title_bar.findViewById(R.id.rg_top);
        rb0=(RadioButton) title_bar.findViewById(R.id.rb0);
        rb1=(RadioButton) title_bar.findViewById(R.id.rb1);
        rb2=(RadioButton) title_bar.findViewById(R.id.rb2);
        rb3=(RadioButton) title_bar.findViewById(R.id.rb3);

        if (root.getChildCount()>0)root.removeAllViews();
        root.addView(title_bar,new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT));
    }

    @Override
    protected void initData() {
     /*   if (!StringUtil.isBlank(SharedPrefUtil.getFleetId())){
            rb0.setText("0"+"\n"+"全部计划(吨)");
            rb1.setText("0"+"\n"+"当日实发(吨)");
            rb2.setText("0"+"\n"+"待发(吨)");
        }else {
            rb0.setText("0"+"\n"+"累计实发(吨)");
            rb1.setText("0"+"\n"+"当日实发(吨)");
            rb2.setText("0"+"\n"+"代运(吨)");
        }*/

        rb0.setText("全部计划(吨)"+"\n"+"0");
        rb1.setText("当日实发(吨)"+"\n"+"0");
        rb2.setText("待发(吨)"+"\n"+"0");


        rb3.setText("在运(吨)"+"\n"+"0");
        filter=new RequestFilter();
        onRefresh();
    }

    @Override
    protected void initListener() {
        rg_top.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                switch (checkedId){
                    case R.id.rb0:
                        showType="1";
                        break;
                    case R.id.rb1:
                        showType="2";
                        break;
                    case R.id.rb2:
                        showType="3";
                        break;
                    case R.id.rb3:
                        showType="4";
                        break;
                }
                filter = new RequestFilter();
                onRefresh();
            }
        });

    }






    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {


        }
    }

    private void getData() {
        DialogUtil.showLoadingDialog(getActivity(),"加载中...");
        Map<String,Object> params=new HashMap<String, Object>();
        params.put("driverId", SharedPrefUtil.getUserId());
     /*   if(StringUtil.isBlank(SharedPrefUtil.getFleetId())){
            params.put("type", "1");
        }else {
            params.put("type", "2");
        }*/
        params.put("fleetId", SharedPrefUtil.getFleetId());
        params.put("type", "2");

        params.put("page", mCurrentPage + "");
        params.put("length", "10");



        if (!StringUtil.isBlank(filter.getStartPoint())){
            params.put("startPoint",filter.getStartPoint());
        }
        if (!StringUtil.isBlank(filter.getEndPoint())){
            params.put("endPoint",filter.getEndPoint());
        }
        if (!StringUtil.isBlank(filter.getStartTime())){
            params.put("startTime",filter.getStartTime());
        }
        if (!StringUtil.isBlank(filter.getEndTime())){
            params.put("endTime",filter.getEndTime());
        }
        //暂时去掉
        if (!StringUtil.isBlank(filter.getSelectType())){
            params.put("selectType",filter.getSelectType());
        }
        if (filter.getOrderStatus2()!=null && filter.getOrderStatus2().size()>0 && filter.getOrderStatus2().size()!=4){
//            params.put("orderStatues",StringUtil.getStatus(filter.getOrderStatus2()));
            params.put("orderStatues",StringUtil.getSplitString(filter.getOrderStatus2()));
        }
        if (filter.getOrderType2()!=null && filter.getOrderType2().size()>0){
            params.put("orderTypes",StringUtil.getSplitString(filter.getOrderType2()));
        }

//当做筛选时，showtype 传0；
        if (params.size()>5){
            params.put("showType", 0);
        }else {
            params.put("showType", showType);
        }


        MyHttpUtil.waitTrans(getActivity(),params, new MyCallback<ResultWaitTransDriver>(getActivity(),new JsonGenericsSerializator()) {
            @Override
            public void onFail(Call call, Exception e, int id) {
                springView.onFinishFreshAndLoad();
                e.printStackTrace();
                PrintUtil.toast(getActivity(), "网络异常，请稍后再试");
            }
            @Override
            public void onSuccess(ResultWaitTransDriver result, int id) {
                springView.onFinishFreshAndLoad();
                if (result.getCode() == ConstUtil.OK) {
                    try {
                        if (result.getData().getData().size()==0){
                            listAdapter.clear();
                            listAdapter.notifyDataSetChanged();
                        }else {
                            setDataResult(result.getData().getData());
                        }

                    } catch (Exception e) {
                        if (result.getMsg()!=null){
                            PrintUtil.toast(activityContext,result.getMsg());
                        }
                        listAdapter.clear();
                        listAdapter.notifyDataSetChanged();
                    }

                }else{
                    springView.onFinishFreshAndLoad();
                    PrintUtil.toast(getActivity(),result.getMsg());
                }
            }
        });
    }

    private void getTopData() {
        DialogUtil.showLoadingDialog(getActivity(),"加载中...");
        Map<String,Object> params=new HashMap<String, Object>();
        params.put("driverId",SharedPrefUtil.getUserId());
        if (!StringUtil.isBlank(SharedPrefUtil.getFleetId())) {
            params.put("fleetId", SharedPrefUtil.getFleetId());
            params.put("type", "2");
        }
//        params.put("driverId", "156");
//        params.put("type", "1");
//        params.put("startTime", TimeUtils.getStartTime());
//        params.put("endTime", TimeUtils.getEndTime());
        MyHttpUtil.waitTransTopInfo(getActivity(),params, new MyCallback<ResultWaitTransTop>(getActivity(),new JsonGenericsSerializator()) {
            @Override
            public void onFail(Call call, Exception e, int id) {
                springView.onFinishFreshAndLoad();
                e.printStackTrace();
                PrintUtil.toast(getActivity(), "网络异常，请稍后再试");
            }
            @Override
            public void onSuccess(ResultWaitTransTop result, int id) {
                if (result.getCode() == ConstUtil.OK) {
                    try {
                        setHeader(result.getData());
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }else{
                    springView.onFinishFreshAndLoad();
                    PrintUtil.toast(getActivity(),result.getMsg());
                }
            }
        });
    }
     private void setHeader(ResultWaitTransTop.DataEntity dispatchInfo) {
        if (dispatchInfo==null)return;
    /*     if (!StringUtil.isBlank(SharedPrefUtil.getFleetId())){
             rb0.setText(StringUtil.getWeight(dispatchInfo.getOrderSumSrplusWeight())+"\n"+"全部计划(吨)");
             rb1.setText(StringUtil.getWeight(dispatchInfo.getCompleteSumDetailsWeightByTime())+"\n"+"当日实发(吨)");
             rb2.setText(StringUtil.getWeight(dispatchInfo.getNotShipmentSumOriginalWeightByTime())+"\n"+"待发(吨)");
         }else {
             rb0.setText(StringUtil.getWeight(dispatchInfo.getWaybillSumDetailsWeight())+"\n"+"累计实发(吨)");
             rb1.setText(StringUtil.getWeight(dispatchInfo.getWaybillSumDetailsWeightByTime())+"\n"+"当日实发(吨)");
             rb2.setText(StringUtil.getWeight(dispatchInfo.getNotShipmentSumOriginalWeightByTime())+"\n"+"代运(吨)");
         }*/


         rb0.setText("全部计划(吨)"+"\n"+StringUtil.getWeight(dispatchInfo.getOrderSumSrplusWeight()));
         rb1.setText("当日实发(吨)"+"\n"+StringUtil.getWeight(dispatchInfo.getCompleteSumDetailsWeightByTime()));
         rb2.setText("待发(吨)"+"\n"+StringUtil.getWeight(dispatchInfo.getNotShipmentSumOriginalWeightByTime()));
        rb3.setText("在运(吨)"+"\n"+StringUtil.getWeight(dispatchInfo.getTransportSumDetailsWeight()));
    }
    @Override
    public void onGetMore() {
        getData();
    }
    @Override
    public void onRefresh() {
        mCurrentPage = 1;
        getData();
        getTopData();
    }

    @Override
    protected void onForceRefresh() {
        onRefresh();
    }

    @Override
    protected boolean isLoadMore() {
        return true;
    }

    @Override
    protected boolean isPullToRefresh() {
        return true;
    }

    public void onEventMainThread(RefleshFragEvent event) {
        if (event.getIndex()==3) {
            onRefresh();
        }
    }


    public void filter(){
        Intent intent=new Intent(activityContext,FilterActivity.class);
        intent.putExtra("type","3");
        intent.putExtra("data",filter);
        startActivity(intent);
    }

    public void onEventMainThread(FilterEvent event) {
        if ("3".equals(event.getType()) ){
            filter=event.getData();
            onRefresh();
        }
    }
}
